//
//  ShareManger.m
//  Weibbo
//
//  Created by 李 林刚 on 13-8-11.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "ShareManger.h"
#import "QQShareCenter.h"


static ShareManger   *shareManger = nil;//第一步：静态实例，并初始化。

@interface ShareManger ()<QQShareCenterDelegate>
{
    SinaWeiboSDK    *sinaOAuth;
    QQShareCenter   *qqOauth;
}

@end

@implementation ShareManger

@synthesize delegate;

+(ShareManger *)SharedInstance //第二步：实例构造检查静态实例是否为nil
{
    @synchronized (self) {
        if (shareManger == nil) {
            shareManger = [[super alloc]init];
        }
    }
    return shareManger;
}

+ (id) allocWithZone:(NSZone *)zone //第三步：重写allocWithZone方法
{
    @synchronized (self) {
        if (shareManger == nil) {
            shareManger = [super allocWithZone:zone];
        }
    }
    return shareManger;
}

- (id) copyWithZone:(NSZone *)zone //第四步
{
    return self;
}

- (id) retain
{
    return self;
}

- (unsigned) retainCount
{
    return UINT_MAX;
}

- (oneway void) release
{
}

- (id) autorelease
{
    return self;
}



- (id)init
{
    @synchronized(self) {
        if (self = [super init])
        {
            sinaOAuth = [[SinaWeiboSDK alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andAppKey: SINA_App_Key andAppSecret:SINA_App_Secret andRedirectURI:RedirectURL delegate:self];
            qqOauth = [[QQShareCenter alloc]initWithDelegate:self];
        }
    }
    return self;
}

#pragma mark - 判断是否绑定
-(BOOL)IsBind:(ShareType)shareType
{
    switch (shareType) {
        case ShareTypeSina:
            return [sinaOAuth isSessionValid];
            break;
        case ShareTypeTencentOAuth:
            return [qqOauth IsQQSessionValid];
            break;
        default:
            return NO;
            break;
    }
}

#pragma mark - 绑定
-(void)LogInWithLogInType:(ShareType)type
{
    switch (type) {
        case ShareTypeSina:
        {
            [sinaOAuth SinaStartAuthorize];
        }
            break;
        case ShareTypeTencentOAuth:
        {
            [qqOauth QQStartAuthorize];
        }
            break;
        default:
            break;
    }
}

#pragma mark - 解除绑定
-(void)LoginOut:(ShareType)type
{
    switch (type) {
        case ShareTypeSina:
        {
            [sinaOAuth SinaLoginOut];
        }
            break;
        case ShareTypeTencentOAuth:
        {
            [qqOauth QQLoginOut];
        }
            break;
        default:
            break;
    }
}

#pragma mark - ShareMessage 第一个为title  第二个为UIImage  第三个为写字先生图片URL

-(void)ShareMessageTitle:(NSString *)title  ShareImage:(UIImage *)image ImageURL:(NSString *)url  ShareType:(int)type;
{
    NSLog(@"%@,%@,%@,%d",title,image,url,type);
    NSArray *contentArr = [[NSArray alloc]initWithObjects:title,image,url, nil];
    if (type==ShareTypeSina) {
        //新浪
        [sinaOAuth shareThread:contentArr];
    }
    if (type==ShareTypeTencentOAuth) {
        [qqOauth shareToQZone:contentArr];
    }
    [contentArr release];
}

#pragma mark - SinaWeiBoSDK Login callback
-(void)SinaWeiboDidLogin:(SinaWeiboSDK *)weiBo
{
    NSLog(@"SinaWeiboDidLogin");
    if ([delegate respondsToSelector:@selector(ShareMangerShareType:DidLoginIn:)]) {
        [delegate ShareMangerShareType:ShareTypeSina DidLoginIn:YES];
    }
}

-(void)SinaWeiboCancelLogin
{
    if ([delegate respondsToSelector:@selector(ShareMangerDidCancelShareType:)]) {
        [delegate ShareMangerDidCancelShareType:ShareTypeSina];
    }
}

-(void)SinaWeiboDidLoginOut
{
    if ([delegate respondsToSelector:@selector(ShareMangerShareType:DidLoginOut:)]) {
        [delegate ShareMangerShareType:ShareTypeSina DidLoginOut:YES];
    }
}

-(void)SinaWeiboDidShareSuccess:(BOOL)isSuceess
{
    if ([delegate respondsToSelector:@selector(ShareMangerShareType:DidShareMessage:)]) {
        [delegate ShareMangerShareType:ShareTypeSina DidShareMessage:isSuceess];
    }
}


#pragma mark - QQShareCenter Login callback
//QQZone登陆成功
- (void)QQDidLogin
{
    if ([delegate respondsToSelector:@selector(ShareMangerShareType:DidLoginIn:)]) {
        [delegate ShareMangerShareType:ShareTypeTencentOAuth DidLoginIn:YES];
    }
}

-(void)QQDidNotLogin:(BOOL)cancelled
{
    if ([delegate respondsToSelector:@selector(ShareMangerDidCancelShareType:)]) {
        [delegate ShareMangerDidCancelShareType:ShareTypeTencentOAuth];
    }
}

/**
 * 登录时网络有问题的回调
 */
- (void)QQDidNotNetWork
{
    if ([delegate respondsToSelector:@selector(ShareMangerDidCancelShareType:)]) {
        [delegate ShareMangerDidCancelShareType:ShareTypeTencentOAuth];
    }
}

/**
 *退出登录
 */
-(void)QQDidLogout
{
    if ([delegate respondsToSelector:@selector(ShareMangerShareType:DidLoginOut:)]) {
        [delegate ShareMangerShareType:ShareTypeTencentOAuth DidLoginOut:YES];
    }
}

/**
 *是否发布成功
 */
-(void)QQDidShareScucess:(BOOL)isSucess
{
    if ([delegate respondsToSelector:@selector(ShareMangerShareType:DidShareMessage:)]) {
        [delegate ShareMangerShareType:ShareTypeTencentOAuth DidShareMessage:isSucess];
    }
}
@end
